package com.xinxing.city.recovery.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.google.gson.Gson;
import com.xinxing.city.recovery.APPRuntime;
import com.xinxing.city.recovery.DI;
import com.xinxing.city.recovery.R;
import com.xinxing.city.recovery.entitys.BucketType;
import com.xinxing.city.recovery.ui.SetEleActivity;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;

import java.util.List;

public class SetSortAdapterTwo extends LinearLayout {

    private List<BucketType> bucketTypes;
    private APPRuntime appRuntime;
    public SetSortAdapterTwo(Context context, List<BucketType> bucketList, Integer position) {
        super(context);
        bucketTypes = bucketList;
        BucketType bucket = bucketList.get(position);
        SetEleActivity setEleActivity = (SetEleActivity) context;
        appRuntime = DI.getInstance(APPRuntime.class);
        LayoutInflater.from(context).inflate(R.layout.item_set_sort,this);

        TextView text = findViewById(R.id.text);
        if(!bucket.getRecycleCataName().equals(""))text.setText(bucket.getName()+"("+bucket.getRecycleCataName()+")");

        ImageView icon1 = findViewById(R.id.icon1);
        ImageView icon2 = findViewById(R.id.icon2);

        if (position == 0) {
            icon1.setVisibility(View.GONE);
        }

        if (position + 1 == bucketList.size()) {
            LayoutParams icon1_params=(LayoutParams)icon1.getLayoutParams();
            icon1_params.setMargins(0,0,80,0);
            icon2.setVisibility(View.GONE);
        }

        Gson gson = new Gson();



        icon1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == 0) return ;

                BucketType one = bucketList.get(position);
                BucketType two = bucketList.get(position - 1);
                bucketTypes.set(position,two);
                bucketTypes.set(position-1,one);
                SharedPreferencesUtils.setParam(context, "bucket", gson.toJson(bucketTypes));
                appRuntime.setBuckes(bucketTypes);
                setEleActivity.initPage();
            }
        });

        icon2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position + 1 == bucketList.size()) return ;

                BucketType one = bucketList.get(position);
                BucketType two = bucketList.get(position + 1);
                bucketTypes.set(position,two);
                bucketTypes.set(position + 1,one);
                SharedPreferencesUtils.setParam(context, "bucket", gson.toJson(bucketTypes));
                appRuntime.setBuckes(bucketTypes);

                setEleActivity.initPage();
            }
        });

    }


}